//
//  HDOperationTools.m
//  HDOperationProject
//
//  Created by Mirko on 15/10/30.
//  Copyright (c) 2015年 Mirko. All rights reserved.
//

#import "HDOperationTools.h"

static NSString *const kOperationRootFolderPath = @"HDOperation";
static NSString *const kOperationResourceFolderPath = @"HDOperationRss";

static NSString *const kOperationAdimageFolderPath = @"ad_image";           // 广告图片文件夹

static NSString *const kOperationAdInfoPlistPath = @"adinfos";              // 广告信息


@implementation HDOperationTools

+(NSString *) getFolderPath:(EOperationFolderType) type operationId:(NSString *) operationId
{
    if (!operationId) {
        return nil;
    }
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) firstObject];
    
    NSString *fullFolderName = nil;
    switch (type) {
        case kOperationFolderTypeRoot:
            fullFolderName = [[docPath stringByAppendingPathComponent:kOperationRootFolderPath] stringByAppendingPathComponent:operationId];
            break;
        case kOperationFolderTypeRss:
            fullFolderName = [[HDOperationTools getFolderPath:kOperationFolderTypeRoot operationId:operationId] stringByAppendingPathComponent:kOperationResourceFolderPath];
            break;
        default:
            break;
    }
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:fullFolderName]) {
        BOOL suss = [[NSFileManager defaultManager] createDirectoryAtPath:fullFolderName withIntermediateDirectories:YES attributes:nil error:nil];
        if (!suss) {
            NSLog(@"创建目录失败！");
        }
    }
    
    return fullFolderName;
}

+(NSString *) getFilePath:(EOperationFileType) type operationId:(NSString *) operationId
{
    switch (type) {
        case kOperationFileTypeActivitiesDataName:
            
            break;
            
        default:
            break;
    }
    return nil;
}

+(NSString *) getActivitiesImageFilePath:(NSString *) imageUrl operationId:(NSString *) operationId
{
    NSString *rssPath = [HDOperationTools getFolderPath:kOperationFolderTypeRss operationId:operationId];
    NSString *filePath = [rssPath stringByAppendingPathComponent:imageUrl];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:nil]) {
        return filePath;
    }
    else
    {
        return nil;
    }
}



/**创建广告图文件夹*/
+(NSString *)getAdImageFolderPath {
    
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) firstObject];
    
    NSString *adImageFold = [docPath stringByAppendingPathComponent:kOperationAdimageFolderPath];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:adImageFold]) {
        
        BOOL suss = [[NSFileManager defaultManager] createDirectoryAtPath:adImageFold
                                              withIntermediateDirectories:YES
                                                               attributes:nil
                                                                    error:nil];
        if (!suss) {
            NSLog(@"创建广告图片目录失败！");
        }
    }
    
    return adImageFold;
}

/**获取广告信息plist文件路径*/
+ (NSString *)getAdInfoPlistPath {
    
    return [[HDOperationTools getAdImageFolderPath] stringByAppendingPathComponent:kOperationAdInfoPlistPath];
}


/**把一个图片拷贝到ad_image目录中*/
+ (BOOL)copyImageToAdimageFoldWithImagePath:(NSString *)filePath newImageName:(NSString *)newImageName{
    
    return [HDOperationTools copyFileWithFilePath:filePath toOtherfolderPath:[HDOperationTools getAdImageFolderPath] newFileName:newImageName];
}

/**把一个文件拷贝到另一个目录中*/
+ (BOOL)copyFileWithFilePath:(NSString *)filePath toOtherfolderPath:(NSString *)folderPath newFileName:(NSString *)newFileName{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL hasFilePath = [fileManager fileExistsAtPath:filePath];
    
    BOOL isDirectory = YES;
    BOOL hasFolderPath = [fileManager fileExistsAtPath:folderPath isDirectory:&isDirectory];
    
    if (hasFilePath & hasFolderPath) {
        
        // 创建目标文件路径
        NSString *dstPath = [folderPath stringByAppendingPathComponent:newFileName];
        
        NSError *error = nil;
        [fileManager moveItemAtPath:filePath toPath:dstPath error:&error];
        
        if (error) {
            
            NSLog(@"移动文件错误: %@", error);
            
            return NO;
        }else {
            
            return YES;
        }
    }
    
    if (!hasFilePath) {
        
        NSLog(@"moveFileToOtherFolder: 没有文件");
    }
    
    if (!hasFolderPath) {
        
        NSLog(@"moveFileToOtherFolder: 没有目录");
    }
    
    return NO;

}

/** 删除某个文件 */
+ (BOOL)removeFileWithPath:(NSString *)path {
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:path]) {
        
        NSError *error = nil;
        
        [fileManager removeItemAtPath:path error:&error];
        
        if (error) {
            
            NSLog(@"删除文件出错: %@", error);
        }else {
            
            return YES;
        }
    }
    
    return NO;
}

/** 删除一组文件 */
+ (BOOL)removeFilesWithPaths:(NSArray *)paths {
    
    NSMutableArray *mutaArr = [paths mutableCopy];
    
    if (mutaArr.count > 0) {
        
        NSString *firstPath = paths.firstObject;
        [mutaArr removeObject:firstPath];
        
       BOOL removeSucess = [HDOperationTools removeFileWithPath:firstPath];
        
        if (removeSucess) {
            
            [HDOperationTools removeFilesWithPaths:[NSArray arrayWithArray:mutaArr]];
        }
    }
    
    return NO;
}

///////// 时间相关

// 获取当前时区的时间戳
+ (NSTimeInterval)getDefaultTimeZoneTimeInterval {
    
    NSDate *now = [NSDate date];
    NSTimeZone *defaultTimeZone = [NSTimeZone defaultTimeZone];
    [NSTimeZone setDefaultTimeZone:defaultTimeZone];
    
    return (now.timeIntervalSince1970 + defaultTimeZone.secondsFromGMT);
}

// 获取，格林威治时区时间戳
+ (NSTimeInterval)getGMTTimeZoneTimeInterval {
    
    NSDate *now = [NSDate date];
    return now.timeIntervalSince1970;
}


@end





























